﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ReadText : MonoBehaviour {

	public struct Choice_NextText{
		public string choice;
		public string nextText;
	};

	public Choice_NextText[] cts;
	public string[] lines;
	public string person;
	public string word;
	public bool isThisLineChoice;
	public int curIndex = 0;


	static char choiceTag = '$';
	static char nextTag = '-';

	/// <summary>
	/// 从path文本文件中读取内容，返回所有行的string	/// </summary>
	/// <returns>The lines of text.</returns>
	/// <param name="path">Path.</param>
	/// <param name="c">C.</param>
	public static string[] readLinesOfText(string path, char c='\n'){
		if (path == "") {
			return null;
		}
		Object o = Resources.Load (path);
		if (o.GetType ().Name == "TextAsset") {
			TextAsset text = o as TextAsset;
			//print (text.text);
			return text.text.Split ('\n');
		}
		return null;
	}
	public static string[] readLinesOfText(TextAsset textAsseet, char c='\n'){
		if (textAsseet != null) {
			return textAsseet.text.Split ('\n');
		}
		return null;
	}
	/// <summary>
	/// 读取一行语句为名字和说话内容	/// </summary>
	/// <returns>The name and word.</returns>
	/// <param name="line">Line.</param>
	/// <param name="c">C.</param>
	public static string[] getNameAndWord(string line, char c='：')
	{
		return line.Split (c);
	}
	/// <summary>
	/// 判断这是否为选择行，如果是，将选项保存到cnts中
	/// </summary>
	/// <returns><c>true</c>, if choice was ised, <c>false</c> otherwise.</returns>
	/// <param name="line">Line.</param>
	/// <param name="cnts">Cnts.</param>
	public static bool isChoice(string line, out Choice_NextText[] cnts){
		if (line.Length > 0 && line [0] == choiceTag) {
			string[] ss = line.Split (choiceTag);
			int i = 0;
			int len = ss.Length-1;
			cnts = new Choice_NextText[len] ;
			//print (cnts.Length);
			foreach (string s in ss) {
				//print (s);
				//第一个会是空字符串
				if (s == "") {
					continue;
				}
				cnts [i].choice = s.Split (nextTag)[0];
				cnts [i++].nextText = s.Split (nextTag)[1];
			}
			return true;
		}
		cnts = null;
		return false;
	}
	/// <summary>
	/// 将一行非选择性文字分开为说话人和所说的话
	/// </summary>
	void LineToWord(string line)
	{
		isThisLineChoice = ReadText.isChoice (line, out cts);
		if (!isThisLineChoice) {
			string[] Person_Word = ReadText.getNameAndWord (line);
			person = Person_Word [0];
			word = Person_Word [1];
		}
	}

	/// <summary>
	/// 读取路径为path的文本文件，存到自身的lines中，成功返回true，失败返回false
	/// </summary>
	/// <returns><c>true</c>, if to lines was texted, <c>false</c> otherwise.</returns>
	/// <param name="path">对话的.txt文件所在的路径</param>
	public bool TextToLines(string path){
		lines = ReadText.readLinesOfText (path);
		if (lines != null) {
			curIndex = 0;
			LineToWord (lines [curIndex]);
			return true;
		}
		return false;
	}
	public bool TextToLines(TextAsset textAsset){
		lines = ReadText.readLinesOfText (textAsset);
		if (lines != null) {
			curIndex = 0;
			LineToWord (lines [curIndex]);
			return true;
		}
		return false;
	}
	/// <summary>
	/// 获取下一行内容，返回false时表示对话结束
	/// 函数执行后如果isThisLineChoice==true，表示这一行为选项，获取到的内容将存到cts中
	/// </summary>
	/// <returns><c>true</c>, if next line was set, <c>false</c> otherwise.</returns>
	public bool setNextLine(){
		if (lines != null && curIndex < lines.Length - 1) {
			LineToWord(lines[++curIndex]);
			return true;
		}else {
			lines = null;
			curIndex = 0;
			isThisLineChoice = false;
		}
		return false;
	}
		
}
